﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDomain;
using QLVMBAYDatacess;
using System.Drawing;

namespace QLVMBAY.Pages
{
    public partial class pThanhToan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session[ClientComm.KEY_THANH_TOAN] = 1;
                var nguoiDung = (NguoiDung)Session[ClientComm.KEY_DANG_NHAP];
                var HanhKhach = HanhKhachDA.Inst.GetByPrimaryKey(false, nguoiDung.MaHanhKhach);
                txtCMND.Text = HanhKhach.CMND;
                txtDiaChi.Text = HanhKhach.DiaChi;
                txtEmail.Text = HanhKhach.Email;
                txtHoChieu.Text = HanhKhach.HoChieu;
                txtHoTen.Text = HanhKhach.TenHanhKhach;
                txtNgaySinh.Text = HanhKhach.NgaySinh;
                txtSoDT.Text  = HanhKhach.DienThoai;
                ddlGTinh.SelectedValue = HanhKhach.GioiTinh.ToString();
                var ttTim=(TTinTimVe)Session[ClientComm.KEY_TIM_VE];
                var itemChieuDi=(GridViewRow)Session[ClientComm.KEY_CHIEU_DI];

                _lblNoiDen.Text += ttTim.TenTPDen;
                _lblNoiDi .Text+= ttTim.TenTPDi;
                _lblTenChBayDi.Text += itemChieuDi.Cells[1].Text;
                var ddlDi = (itemChieuDi.FindControl("ddlLoaiVe") as DropDownList);
                var getItem = ChiTietCBayDA.Inst.GetByPrimaryKey(false, ddlDi.SelectedValue);
                _lblTenLoaiGheDi.Text = ddlDi.SelectedItem.Text.Split('-')[1];
                _lblTenLoaiVeDi.Text = ddlDi.SelectedItem.Text.Split('-')[0];
                _lblThGianDen.Text += (itemChieuDi.FindControl("lblThoiGianBay") as Label).Text;
                _lblThGianDi.Text += (itemChieuDi.FindControl("lblTgKhoiHanh") as Label).Text + " " + ttTim.ThoiGianDi.ToShortDateString();

                decimal TongVeDi = getItem .GiaVe* (ttTim.SLVe + ttTim.SLVeTrEm);
                decimal TongVe = 0;
                if (ttTim.IsKhuHoi)
                {
                    pnChieuVe.Visible = true;
                    var itemChieuVe = (GridViewRow)Session[ClientComm.KEY_CHIEU_VE];

                    lblNoiDen.Text += ttTim.TenTPDi;
                    lblNoiDi.Text += ttTim.TenTPDen;
                    lblTenChBay.Text += itemChieuVe.Cells[1].Text;
                    ddlDi = (itemChieuVe.FindControl("ddlLoaiVe") as DropDownList);
                    lblTenLoaiGhe.Text = ddlDi.SelectedItem.Text.Split('-')[1];
                    lblTenLoaiVe.Text = ddlDi.SelectedItem.Text.Split('-')[0];
                    lblThGianDen.Text += (itemChieuVe.FindControl("lblThoiGianBay") as Label).Text;
                    lblThGianDi.Text += (itemChieuVe.FindControl("lblTgKhoiHanh") as Label).Text + " " + ttTim.ThoiGianDi.ToShortDateString();
                    getItem = ChiTietCBayDA.Inst.GetByPrimaryKey(false, ddlDi.SelectedValue);
                    TongVe = getItem.GiaVe * (ttTim.SLVe + ttTim.SLVeTrEm);
                }
                lblThanhToan.Text = "Tổng thanh toán: " + (TongVeDi + TongVe).ToString("#,##0") + " VNĐ";
                btnHuy.Enabled = true;
                btnLuu.Enabled = true;
            }

        }

        protected void lnkTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx");
        }

        protected void lnkChuyenBay_Click(object sender, EventArgs e)
        {
            Response.Redirect("/pTimChuyenBay.aspx");
        }

        protected void lnkThanhToan_Click(object sender, EventArgs e)
        {
            Response.Redirect("/pThanhToan.aspx");
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Session[ClientComm.KEY_THANH_TOAN] = null;
            Session[ClientComm.KEY_TIM_VE] = null;
            Session[ClientComm.KEY_CHIEU_DI] = null;
            Session[ClientComm.KEY_CHIEU_VE] = null;
            Response.Redirect("~/Default.aspx");
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            var itemChieuDi = (GridViewRow)Session[ClientComm.KEY_CHIEU_DI];
             var ttTim=(TTinTimVe)Session[ClientComm.KEY_TIM_VE];
            var nguoiDung = (NguoiDung)Session[ClientComm.KEY_DANG_NHAP];
            var LstDatCho = new List<DatCho>();
            string MaVe = "VAL" + new Random().Next(1, 9999);
            string MAC = "BK" + new Random().Next(1, 9999);
            var DatCho = GetItem(itemChieuDi,MAC+"-1",MaVe, nguoiDung.MaHanhKhach, ttTim.SLVe + ttTim.SLVeTrEm, ttTim.IsKhuHoi, LUOT_DAT_CHO.CHIEU_DI);
            LstDatCho.Add(DatCho);
            if (ttTim.IsKhuHoi)
            {
                var itemChieuVe = (GridViewRow)Session[ClientComm.KEY_CHIEU_VE];
                var DatCho1 = GetItem(itemChieuVe,MAC+"-2",MaVe, nguoiDung.MaHanhKhach, ttTim.SLVe + ttTim.SLVeTrEm, ttTim.IsKhuHoi, LUOT_DAT_CHO.CHIEU_VE);
                LstDatCho.Add(DatCho1);
            }
            var Res = DatchoDA.Inst.ThemDatCho(LstDatCho);
            if (Res)
            {
                lblStatus.Text = "Thông vé được lưu thành công. Mã vé: " + MaVe;
                lblStatus.ForeColor = Color.Blue;
                btnHuy.Enabled = false;
                btnLuu.Enabled = false;
            }
            else
            {
                lblStatus.Text = "Lỗi xảy ra, dữ liệu chưa được lưu.";
                lblStatus.ForeColor = Color.Red;
            }
            lblStatus.Visible = true;
        }
        DatCho GetItem(GridViewRow Item,string MaDC,string MaVe, string MaHK, int SLVe,bool isKhuHoi, LUOT_DAT_CHO luotDCho)
        {
            var DatCho = new DatCho();
            var ddlDi = (Item.FindControl("ddlLoaiVe") as DropDownList);
            var getItem = ChiTietCBayDA.Inst.GetByPrimaryKey(false, ddlDi.SelectedValue);
            DatCho.ItemChiTietCBay = new ChiTietChuyenBay() { MaCTCBay =int.Parse( ddlDi.SelectedValue) };
            DatCho.MaVe = MaVe;

            DatCho.ItemHanhKhach = new HanhKhach() { MaHanhKhach = MaHK };
            DatCho.LoaiDatCho = isKhuHoi ? LOAI_DAT_CHO.KHU_HOI : LOAI_DAT_CHO.MOT_CHIEU;
            DatCho.LuotDatCho = luotDCho;
            DatCho.MaDatCho = MaDC;
            DatCho.PhiHuy = 0;
            DatCho.SLVe = SLVe;
            DatCho.TongTien = getItem.GiaVe * SLVe;
            return DatCho;
        }

    }
}